package com.fruit.controller.wap.lightpay;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.fruit.constants.PayConstant;
import com.fruit.controller.base.BaseController;
import com.fruit.pojo.pay.TransactionFlow;
import com.fruit.resolver.DefiniteException;
import com.fruit.service.lightpay.LightPayManager;
import com.fruit.util.ResultInfo;
import com.fruit.util.ResultUtil;

/**
 * 支付结果controller
 *
 * @author shibeilei
 * @version 2017.11.30 12:20
 */
@Controller
@RequestMapping(value = "/wap/result")
public class LightPayResultController extends BaseController {

	@Autowired
	private LightPayManager lightPayService;

	@Value("${domain}")
	private String domain;

	/**
	 * 支付结果 <br/>
	 *
	 * @param code
	 *            信息
	 * @return 支付结果页
	 * @author shibeilei
	 * @version 2017.11.30 11:40
	 */
	@RequestMapping(method = { RequestMethod.POST, RequestMethod.GET })
	public ModelAndView lightPayCashier(String code) {
		this.logBefore(logger, "pay result");
		ModelAndView view = this.getModelAndView();

		Map<String, Object> resMap = lightPayService.getLightpayCashierByCode(code);

		ResultInfo ri = (ResultInfo) resMap.get("result");
		if (ResultInfo.ERROR.equals(ri.getState())) {
			throw new DefiniteException(ri.getMessage());
		}

		TransactionFlow tf = (TransactionFlow) resMap.get("tf");
		logger.debug("check pay result is : {}", resMap);
		if (PayConstant.State.pay.equals(tf.getState())) {

			view.addObject("state", "01");
			view.addObject("price", tf.getPayPrice());
		} else {
			view.addObject("state", "00");
		}

		if (PayConstant.Type.consume.equals(tf.getType())) {
			logger.debug("redirect to lightpay/result");
			view.addObject("type", tf.getType());
			view.setViewName("lightpay/result");
		}

		return view;
	}

	/**
	 * 支付结果 <br/>
	 *
	 * @param code
	 *            信息
	 * @return 支付结果页 wap版
	 * @author shibeilei
	 * @version 2017.11.30 11:40
	 */
	@RequestMapping(value = "wap", method = { RequestMethod.POST, RequestMethod.GET })
	public ModelAndView result(String code) {
		this.logBefore(logger, "pay result");
		ModelAndView view = this.getModelAndView();
		view.addObject("code", code);
		view.setViewName("lightpay/resultWap");

		return view;
	}

	@RequestMapping(value = "getResult", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Map<String, Object> getResult(String code) {
		Map<String, Object> resMap = lightPayService.getLightpayCashierByCode(code);

		ResultInfo ri = (ResultInfo) resMap.get("result");
		if (ResultInfo.ERROR.equals(ri.getState())) {
			return resMap;
		}
		
		TransactionFlow tf = (TransactionFlow) resMap.get("tf");
		logger.debug("check pay result is : {}", resMap);
		if (PayConstant.State.pay.equals(tf.getState())) {
			resMap.put("state", "01");
			resMap.put("price", tf.getPayPrice());
		} else {
			resMap.put("state", "00");
		}
		
		return resMap;

	}
}
